package com.example.book.config;

import com.example.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/22
 * Time: 16:55
 */
@ControllerAdvice
@Slf4j
@ResponseBody
public class ExceptionAdvice {

    @ExceptionHandler
    public Object handler(Exception e) {
        log.error("发生异常, e: " + e);
        return Result.fail();
    }

//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    public Object handler(ArrayIndexOutOfBoundsException e) {
        log.error("发生异常, e: " + e);
        return Result.fail("发生数组越界异常");
    }

    @ExceptionHandler
    public Object handler(NullPointerException e) {
        log.error("发生异常, e: " + e);
        return Result.fail("发生空指针异常");
    }


}
